var isClickPage
var isIntPage =false;
function getProductList(page){
  var type = getQueryString('type')
  if (!type) {
    type=0
  }
  $.ajax({
    type: 'POST',
    url: ajaxUrlObj['GET_PRODUCT_LIST'],
    data: {page:1,rows:1000,type:type},
    data: {
      page: page,
      rows: 4,
      type:type
    },
    success: function(rep){
      if (typeof rep === 'string') {
        rep = JSON.parse(rep)
      }
      if (rep.code == 1){
        var data = typeof rep.data === 'string' ? JSON.parse(rep.data).rows : rep.data.rows
        var str = ''
        if(!isIntPage){
          isIntPage =true;
          intPage(rep.total, 4)
        }
        for(var i =0,l=data.length;i<l;i++){
          str+='<a href="product_details.html?id='+data[i].id+'" class="item-box"> <div class="fixed-pic-h"> <img src="'+host+data[i].productImageList[0].imageUrl+'"> </div> <p class="title">'+data[i].productName+'</p> </a>'
        }
        $("#productListBox").html(str)
      }
    },
    error: function(error){
      console.log(error)
    }
  });
}
function intPage(total, pageSize) {
  var page = total > 0 ? Math.ceil(total / pageSize) : 1
  var str = ''
  for(var i =1;i<=page;i++){
    str+='<a class="page-item">'+i+'</a>'
  }
  $(".page-box").html(str)
  $("#totalPage").html(page)
  $(".pre-btn").on("click",function(){
    var page = parseInt($("#currentPage").html())
    if (page <= 1) return
    getProductList(page-1)
  })
  $(".next-btn").on("click",function(){
    var page = parseInt($("#currentPage").html())
    var all = $("#totalPage").html()
    if (page >= all) return
    getProductList(page+1)
  })
  $(".page-item").on("click",function(){
    var page = parseInt($(this).html())
    getProductList(page)
    $(".page-box").addClass("hide")
  })
}
function getProductNavBar (){
  fetchApi({
    type:"GET",
    apiKey:'GET_TREE',
    successCb:function(data){
      var str = getProductNavBarDom(data)
      $(".menu-main").html(str)
      var tempNav = []
      for(var i =0,l=allProductNav.length;i<l;i++){
        tempNav[allProductNav[i].id] = allProductNav[i]
      }
      allProductNav = tempNav
      $("#currentProduct").html(allProductNav[getQueryString('type')].text)
    }
  })
}
var allProductNav = []
function getProductNavBarDom (data) {
  var str = ''
  allProductNav = allProductNav.concat(data)
  for(var i=0,l=data.length;i<l;i++){
    if(data[i].children &&data[i].children.length>0){
      str += '<div class="menu-item-box"><div class="menu-item"><i class="iconfont"></i><a href="products.html?type='+data[i].id+'">'+data[i].text+'</a></div><div class="menu-item-child">'
      str+=getProductNavBarDom(data[i].children)
      str+= '</div></div>'
    }else{
      str += '<a href="products.html?type='+data[i].id+'" class="child-item">'+data[i].text+'</a>'
    }
  }
  return str
}
$(window).ready(function() {
  getProductList(1)
  getProductNavBar()
  $(".num-box").on("touchstart",function(e){
    if($(e.target).hasClass("num-box")){
      var $page = $(".page-box")
      isClickPage = true
      if($page.hasClass("hide")){
        $page.removeClass("hide")
      } else{
        $page.addClass("hide")
      }
    }
  })
  $(window).on("touchstart",function(e){
    if(!$(e.target).hasClass("page-item")&&!$(e.target).hasClass("page-box")){
      if(!isClickPage&&!$(".page-box").hasClass("hide")) {
        $(".page-box").addClass("hide")
      } else{
        isClickPage=false
      }
    }
  })
  $(".top-info-box .iconfont").on("click",function(){
    $(".p-menu-box").addClass("show-p-menu")
  })
  $(".p-menu-box .back-menu").on("click",function(){
    $(".p-menu-box").removeClass("show-p-menu")
  })
  $(".menu-main").on("click",".menu-item>.iconfont",function(){
    var $box=$(this).parents(".menu-item-box").eq(0)
    $box.hasClass("show-chilid-item") ? $box.removeClass("show-chilid-item") : $box.addClass("show-chilid-item")
  })
})